﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Application_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Guid _applicationGuid = Guid.Empty;

            if (Request.QueryString["ApplicationGuid"] != null)
            {
                _applicationGuid = new Guid(Request.QueryString["ApplicationGuid"]);
            }

            if (_applicationGuid != Guid.Empty)
            {
                Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

                System.Data.DataTable applicationTable = moduleProvider.ApplicationGetByApplicationGuid(_applicationGuid);

                if (applicationTable.Rows.Count == 1)
                {
                    System.Data.DataRow applicationRow = applicationTable.Rows[0];

                    applicationIdTextBox.Text = System.Convert.ToString(applicationRow["ApplicationId"]);
                    applicationGuidTextBox.Text = System.Convert.ToString(applicationRow["ApplicationGuid"]);
                    orderTextBox.Text = System.Convert.ToString(applicationRow["ApplicationOrder"]);
                    visibleCheckBox.Checked = System.Convert.ToBoolean(applicationRow["ApplicationVisible"]);

                    applicationNameTextBox.Text = System.Convert.ToString(applicationRow["ApplicationName"]);
                    applicationDescriptionTextBox.Text = System.Convert.ToString(applicationRow["ApplicationDescription"]);
                }
            }

            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            System.Data.DataTable roleTable = securityProvider.RoleGetList();

            groupDropDownList.DataSource = roleTable;
            groupDropDownList.DataTextField = "RoleName";
            groupDropDownList.DataValueField = "RoleGuid";

            groupDropDownList.DataBind();

            System.Data.DataTable userRoleTable = securityProvider.RoleGetByApplicationGuid(_applicationGuid);

            groupListBox.DataSource = userRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            switch (formMode)
            {
                case FormMode.Create:
                    applicationNameTextBox.Enabled = true;
                    applicationDescriptionTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    visibleCheckBox.Enabled = true;

                    applicationNameTextBox.ReadOnly = false;
                    applicationDescriptionTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    applicationNameTextBox.Enabled = true;
                    applicationDescriptionTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    visibleCheckBox.Enabled = true;

                    applicationNameTextBox.ReadOnly = true;
                    applicationDescriptionTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    applicationNameTextBox.Enabled = true;
                    applicationDescriptionTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    visibleCheckBox.Enabled = true;

                    applicationNameTextBox.ReadOnly = false;
                    applicationDescriptionTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    applicationNameTextBox.Enabled = true;
                    applicationDescriptionTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    visibleCheckBox.Enabled = true;

                    applicationNameTextBox.ReadOnly = true;
                    applicationDescriptionTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.ApplicationUpdate(
                new Guid(applicationGuidTextBox.Text),
                System.Convert.ToInt32(applicationIdTextBox.Text),
                applicationNameTextBox.Text,
                applicationDescriptionTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text),
                visibleCheckBox.Checked))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.ApplicationInsert(
                Guid.NewGuid(),
                applicationNameTextBox.Text,
                applicationDescriptionTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text),
                visibleCheckBox.Checked) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        UnsubscribeButton.Enabled = groupListBox.SelectedIndex != -1;
    }
    protected void subscribeButton_Click(object sender, EventArgs e)
    {
        Guid _applicationGuid = Guid.Empty;

        if (Request.QueryString["ApplicationGuid"] != null)
        {
            _applicationGuid = new Guid(Request.QueryString["ApplicationGuid"]);
        }

        if (_applicationGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.ApplicationRoleInsert(_applicationGuid, new Guid(groupDropDownList.SelectedValue));

            System.Data.DataTable applicationRoleTable = securityProvider.RoleGetByApplicationGuid(_applicationGuid);

            groupListBox.DataSource = applicationRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();
        }
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e)
    {
        Guid _applicationGuid = Guid.Empty;

        if (Request.QueryString["ApplicationGuid"] != null)
        {
            _applicationGuid = new Guid(Request.QueryString["ApplicationGuid"]);
        }

        if (_applicationGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.ApplicationRoleDelete(_applicationGuid, new Guid(groupListBox.SelectedValue));

            System.Data.DataTable applicationRoleTable = securityProvider.RoleGetByApplicationGuid(_applicationGuid);

            groupListBox.DataSource = applicationRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            UnsubscribeButton.Enabled = false;
        }
    }
}
